﻿using System;
// 数组版本
namespace CardLib
{
	public class DeckArray
	{
		private Card[] cards;

		public DeckArray()
		{
			cards = new Card[52];
			
			for(int suitVal = 0; suitVal < 4; suitVal ++ )
			{
				for (int rankVal = 1; rankVal < 14; rankVal++)
				{
					// int vale = suitVal * 13 + rankVal;
					// Console.WriteLine(vale);
					cards[suitVal*13 + rankVal - 1] = new Card((Suit)suitVal, (Rank)rankVal);
				}
			}
		}

		public Card GetCard(int cardNum)
		{
			if ((cardNum >= 0)&&(cardNum <= 51))
			{
				return cards[cardNum];
			}
			throw new System.ArgumentOutOfRangeException("cardNum", cardNum, "value out");
		}

		public void Shuffle()
		{
			Card []newCards = new Card[52];
			bool []records = new bool[52];
		
			Random ranGen = new Random();
			for (int i = 0; i < 52; i++)
			{
				while (true)
				{
					int ranNum = ranGen.Next(52);

					if (records[ranNum] == false)
					{
						newCards[i] = cards[ranNum];
						records[ranNum] = true;
						break;
					}
				}
			}
			newCards.CopyTo(cards, 0);
		}
	}
}